//
//  UserInfoViewController.m
//  F50
//
//  Created by WN on 15/5/13.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "UserInfoViewController.h"
#import "LoginViewController.h"
@interface UserInfoViewController ()<UITextFieldDelegate>

@end

@implementation UserInfoViewController


- (void)createBackground
{
    self.navigationController.navigationBarHidden = YES;
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bgView.image = [UIImage imageNamed:@"background"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    //中间标题
    UILabel *  nav_title=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 20, 160, 40)];
    nav_title.text=@"填写个人信息";
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font=[UIFont boldSystemFontOfSize:20];
    nav_title.textColor=[UIColor whiteColor];
    nav_title.backgroundColor=[UIColor clearColor];
    [bgView addSubview:nav_title];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(20, 32, 15, 18);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:backBtn];
    
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15, 80, WIDTH - 30, 100)];
    view.layer.cornerRadius = 8;
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [bgView addSubview:view];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, WIDTH - 30, 1)];
    label.backgroundColor = RGB(204, 204, 210);
    [view addSubview:label];
    
    UILabel *phoneLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 30)];
    phoneLab.text = @"姓名";
    [view addSubview:phoneLab];
    
    UILabel *passLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 50, 30)];
    passLab.text = @"密码";
    [view addSubview:passLab];
    
    self.name = [[UITextField alloc]initWithFrame:CGRectMake(60, 10, WIDTH - 90, 30)];
    self.name.placeholder = @"请输入你的姓名";
    self.name.delegate = self;
    [view addSubview:self.name];
    
    self.password = [[UITextField alloc]initWithFrame:CGRectMake(60, 60, WIDTH - 90, 30)];
    self.password.placeholder = @"请填写密码";
    self.password.delegate = self;
    [view addSubview:self.password];
    
    UIButton *regest = [UIButton buttonWithType:UIButtonTypeCustom];
    regest.frame = CGRectMake(15, 200, WIDTH - 30, 40);
    [regest addTarget:self action:@selector(doRegest) forControlEvents:UIControlEventTouchUpInside];
    [regest setBackgroundImage:[UIImage imageNamed:@"register"] forState:UIControlStateNormal];
    [regest setTitle:@"注册" forState:UIControlStateNormal];
    regest.layer.cornerRadius = 8;
    [regest setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bgView addSubview:regest];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self createBackground];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)doRegest{
    
    
    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"registNum"],@"mobile",self.name.text,@"name",self.password.text,@"password",@"ios",@"osType",nil];
    NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"member",@"class",@"registration",@"func",date,@"param", nil];
   
    [[AFCustomObject shareAFCustomObject] registrAtionWithParams:pram success:^(NSDictionary *result) {
        NSLog(@"%@",result);
        NSLog(@"%@",[result objectForKey:@"errMsg"]);
        self.tabBarController.tabBar.hidden = NO;
        [self.navigationController popToRootViewControllerAnimated:YES];
        //登录
        NSDictionary * date1 = [[NSDictionary  alloc]initWithObjectsAndKeys:[USER_DEFAULT objectForKey:@"registNum"],@"mobile",self.password.text,@"password",@"ios",@"osType",nil];
        NSDictionary *pram1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"member",@"class",@"login",@"func",date1,@"param", nil];
        
        [[AFCustomObject shareAFCustomObject] loginActionWithParams:pram1
                                                            success:^(NSDictionary *result) {
            NSLog(@"%@",result);
            NSLog(@"%@",[result objectForKey:@"errMsg"]);
            
            NSDictionary *dic = [result objectForKey:@"result"];
            [USER_DEFAULT setObject:[dic objectForKey:@"phone"] forKey:@"phone"];
            [USER_DEFAULT setObject:[dic objectForKey:@"token"] forKey:@"token"];
            [USER_DEFAULT setObject:[dic objectForKey:@"name"] forKey:@"name"];
            [USER_DEFAULT setObject:[dic objectForKey:@"relations"] forKey:@"relations"];

            [USER_DEFAULT synchronize];
        } fail:^{
        }];
        
        
        
        
    } fail:^{
        NSLog(@"1111111");
    }];
   
}

- (void)back{
//    [self.navigationController popToRootViewControllerAnimated:YES];
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
